package com.dongdongshop.service;


import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class TbGoodsServiceImpl implements TbGoodsService{

    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;



    @Override
    public boolean insertByGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc) {
        tbGoodsMapper.insertSelective(tbGoods);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        tbGoodsDescMapper.insertSelective(tbGoodsDesc);
        return true;
    }

    @Override
    public List<TbGoods> selectAll(TbGoods tbGoods) {
        List<TbGoods> goods = tbGoodsMapper.selectBy(tbGoods);
        return goods;
    }

    @Override
    public boolean updateByPass(Long[] ids) {
        int i = tbGoodsMapper.updateByPass(ids);
        //成功审核后使用mq发送消息
        try{
            if (i > 0){
                rocketMQTemplate.convertAndSend("dongdong",ids);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return i>0;
    }

    @Override
    public boolean updateByNoPass(Long[] ids) {
        int i = tbGoodsMapper.updateByNoPass(ids);
        return i>0;
    }

    @Override
    public boolean deleteBy(Long[] ids) {
        int i = tbGoodsMapper.deleteBy(ids);
        return i>0;
    }

    @Override
    public TbGoods selectGoodsById(Long goodsId) {

        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(goodsId);
        return tbGoods;
    }
}
